package com.aisile.user.service.impl;

import com.aisile.common.exception.CustomExceptionThrows;
import com.aisile.common.yongyou.YongYouCommonScan;
import com.aisile.model.common.dtos.PageResponseResult;
import com.aisile.model.common.dtos.ResponseResult;
import com.aisile.model.common.enums.AppHttpCodeEnum;
import com.aisile.model.user.dtos.AuthDto;
import com.aisile.model.user.pojos.ApUserRealname;
import com.aisile.user.config.RabbitConfig;
import com.aisile.user.mapper.ApUserRealnameMapper;
import com.aisile.user.service.ApUserRealnameService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Auther:yry
 * @Date:2024/7/17 0017
 * @VERSON:1.0
 */
@Service
public class ApUserRealnameImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private YongYouCommonScan yongYouCommonScan;

    @Override
    public ResponseResult loadListByStatus(AuthDto dto) {
        // 在后台定死状态 = 3
        if (dto == null) {
            CustomExceptionThrows.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 分页初始化
        dto.checkParam();
        LambdaQueryWrapper<ApUserRealname> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (dto.getStatus() == null) {
            dto.setStatus((short) 3); // 默认人工审核
        }
        // 只能查 状态 = 0 1 2 3 9
        if ((dto.getStatus() >= (short) 0 && dto.getStatus() <= (short) 3) || (dto.getStatus().equals((short) 9))) {
            lambdaQueryWrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        } else {
            CustomExceptionThrows.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        IPage<ApUserRealname> iPage =  this.page(new Page<>(dto.getPage(),dto.getSize()),lambdaQueryWrapper);
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),((Long)iPage.getTotal()).intValue());
        responseResult.setData(iPage.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult updateStatusById(AuthDto dto, Short type) {
        if (dto == null || dto.getId() == null) {
            CustomExceptionThrows.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (type == null) {
            CustomExceptionThrows.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        ApUserRealname apUserRealname = this.getById(dto.getId());
        if (apUserRealname == null) {
            CustomExceptionThrows.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (dto.getMsg() != null) {
            apUserRealname.setReason(dto.getMsg());
        }
        apUserRealname.setStatus(type);
        this.updateById(apUserRealname);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    public void autoUpdateStatusById(AuthDto dto) {
        if (dto == null || dto.getId() == null) {
            return;
        }
        // 整个审核的过程中 其实就是在修改状态  状态  0 创建中  1 待审核  2 审核失败  3.机器审核失败，待人工审核 9 审核通过
        // 先获取用户提交的审核数据
        ApUserRealname apUserRealname = this.getById(dto.getId());
        if (apUserRealname != null && apUserRealname.getStatus().equals((short) 1)) {
            // 需要审核
            int i = 0;
            Map map = yongYouCommonScan.idCardOCRSacn(apUserRealname.getFontImage());
            if (map == null || map.get("data") == null) {
                // 审核失败！
                return;
            }
            Map data = (Map) map.get("data");
            if (data.get("name") == null || data.get("cardNum") == null) {
                this.updateStatusById(dto, (short) 2);
                return;
            }
            // 身份证二要素
            if (!yongYouCommonScan.matchIdentityScan(apUserRealname.getName(), apUserRealname.getIdno(), 1)) {
                // 审核失败！
                this.updateStatusById(dto, (short) 2);
                return;
            }
            int bioNum = yongYouCommonScan.BiologicalexaminationScan(apUserRealname.getLiveImage(), 1);
            // 1  0  -1
            if (bioNum < 0) {
                // 审核失败
                this.updateStatusById(dto, (short) 2);
                return;
            } else if (bioNum > 0) {
                i++;
            }

            int perNum = yongYouCommonScan.personandcertificateverification(1);
            if (perNum < 0) {
                // 审核失败
                this.updateStatusById(dto, (short) 2);
                return;
            } else if (perNum > 0) {
                i++;
            }

            // 审核成功 或者 疑似
            if (i == 0) {
                // 通过
                this.updateStatusById(dto, (short) 9);
            } else {
                // 疑似
                this.updateStatusById(dto, (short) 3);
            }
        }

    }

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public ResponseResult saveUserRealname(ApUserRealname apUserRealname) {
        // 1. 保存数据到mysql 数据库即可
        // this.save(apUserRealname);
        // 采用异步方式显现 自动审核
        // rabbitMQ 的生产者
        rabbitTemplate.convertAndSend(RabbitConfig.AUTH_EXCHANGE, "smrz", apUserRealname.getId());
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
